package com.fjwt.gz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.OfficialReplyDTO;
import com.fjwt.gz.db.entity.OfficialReplyEntity;
import com.fjwt.gz.db.vo.OfficialReplyVO;
import com.fjwt.gz.service.mapper.OfficialReplyMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 公众号自动回复表 服务实现类
 * </p>
 *
 * @author shiyonghao
 * @since 2025-04-09
 */
@Slf4j
@Service
public class OfficialReplyService extends ServiceImpl<OfficialReplyMapper, OfficialReplyEntity> {

    /**
     * 公众号自动回复分页
     * @param page
     * @param dto
     * @return
     */
    public IPage<OfficialReplyVO> getPage(IPage page,OfficialReplyDTO dto) {
        if (StringUtils.isEmpty(dto.getOfficialAppId())) {
            throw new BizException("请求参数缺失！");
        }
        LambdaQueryWrapper<OfficialReplyEntity> queryWrapper = OfficialReplyEntity.gw()
                .eq(OfficialReplyEntity::getOfficialAppId, dto.getOfficialAppId())
                .eq(OfficialReplyEntity::getIsDeleted, 0)
                .like(OfficialReplyEntity::getReqKeyword, StringUtils.isNotEmpty(dto.getReqKeyword()) ? dto.getReqKeyword() : "");
        IPage<OfficialReplyVO> pageVo = this.baseMapper.selectPage(page, queryWrapper);
        return pageVo;
    }

    /**
     * 添加公众号自动回复
     * @param dto
     * @param sysUserId
     */
    public void insert(OfficialReplyDTO dto,Long sysUserId) {
        dto.setCreatedAt(new Date());
        dto.setCreatedUid(sysUserId);
        LambdaQueryWrapper<OfficialReplyEntity> gw = OfficialReplyEntity.gw();
        gw.eq(OfficialReplyEntity::getOfficialAppId, dto.getOfficialAppId());
        gw.eq(OfficialReplyEntity::getReqKeyword, dto.getReqKeyword());
        OfficialReplyEntity one = getOne(gw);
        OfficialReplyEntity entity = BeanUtil.copyProperties(dto, OfficialReplyEntity.class);
        if (null == one) {
            boolean flag = save(entity);
            if (!flag){
                throw new BizException("操作失败！");
            }
        } else {
            one.setIsDeleted(0);
            boolean flag = updateById(one);
            if (!flag){
                throw new BizException("操作失败！");
            }
        }

    }

    /**
     * 删除公众号自动回复
     * @param replyId
     */
    public void removeReply(Long replyId) {
        if (null == replyId) {
            throw new BizException("请求参数缺失！");
        }
        boolean flag =
                this.lambdaUpdate()
                        .set(OfficialReplyEntity::getIsDeleted, 1)
                        .eq(OfficialReplyEntity::getReplyId, replyId)
                        .update();
        if (!flag){
            throw new BizException("操作失败！");
        }

    }

    /**
     * 修改公众号自动回复
     * @param dto
     */
    public void updateReply(OfficialReplyDTO dto) {
        if (null == dto.getReplyId()) {
            throw new BizException("请求参数缺失！");
        }
        OfficialReplyEntity entity = BeanUtil.copyProperties(dto, OfficialReplyEntity.class);
        boolean flag = updateById(entity);
        if (!flag){
            throw new BizException("操作失败！");
        }
    }

    /**
     * 获取公众号自动回复详情
     * @param replyId
     * @return
     */
    public OfficialReplyVO getReplyDetail(Long replyId) {
        if (null == replyId) {
            throw new BizException("请求参数缺失！");
        }
        OfficialReplyEntity entity = getById(replyId);
        OfficialReplyVO vo = BeanUtil.copyProperties(entity, OfficialReplyVO.class);
        return vo;
    }
}
